from django.db import models
from dvadmin.utils.models import CoreModel

class Supplier(CoreModel):
    """供应商信息表"""
    name = models.CharField(max_length=100, verbose_name="供应商名称")
    code = models.CharField(max_length=50, unique=True, verbose_name="供应商编码")
    contact_person = models.CharField(max_length=50, verbose_name="联系人")
    contact_phone = models.CharField(max_length=20, verbose_name="联系电话")
    email = models.EmailField(verbose_name="电子邮箱", null=True, blank=True)
    address = models.CharField(max_length=200, verbose_name="地址")
    business_scope = models.TextField(verbose_name="经营范围", null=True, blank=True)
    status = models.BooleanField(default=True, verbose_name="状态")
    credit_rating = models.CharField(max_length=20, verbose_name="信用等级", null=True, blank=True)
    remarks = models.TextField(verbose_name="备注", null=True, blank=True)

    class Meta:
        db_table = "supplier"
        verbose_name = "供应商信息"
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)

class SupplierEvaluation(CoreModel):
    """供应商评估记录表"""
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE, verbose_name="供应商")
    evaluation_date = models.DateField(verbose_name="评估日期")
    quality_score = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="质量评分")
    delivery_score = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="交付评分")
    price_score = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="价格评分")
    service_score = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="服务评分")
    total_score = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="总评分")
    evaluator = models.CharField(max_length=50, verbose_name="评估人")
    evaluation_content = models.TextField(verbose_name="评估内容")

    class Meta:
        db_table = "supplier_evaluation"
        verbose_name = "供应商评估"
        verbose_name_plural = verbose_name
        ordering = ('-evaluation_date',)

class SupplierProduct(CoreModel):
    """供应商产品信息表"""
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE, verbose_name="供应商", related_name="products")
    product_name = models.CharField(max_length=100, verbose_name="产品名称")
    product_code = models.CharField(max_length=50, verbose_name="产品编码")
    category = models.CharField(max_length=50, verbose_name="产品类别", null=True, blank=True)
    specification = models.CharField(max_length=100, verbose_name="规格型号", null=True, blank=True)
    unit = models.CharField(max_length=20, verbose_name="计量单位")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="价格")
    min_order_quantity = models.IntegerField(verbose_name="最小订购量", default=1)
    lead_time = models.IntegerField(verbose_name="供货周期(天)", null=True, blank=True)
    quality_standard = models.CharField(max_length=100, verbose_name="质量标准", null=True, blank=True)
    is_active = models.BooleanField(default=True, verbose_name="是否有效")
    description = models.TextField(verbose_name="产品描述", null=True, blank=True)
    remarks = models.TextField(verbose_name="备注", null=True, blank=True)

    class Meta:
        db_table = "supplier_product"
        verbose_name = "供应商产品信息"
        verbose_name_plural = verbose_name
        ordering = ('supplier', 'product_name')
        unique_together = ('supplier', 'product_code')
